package com.xiaotu.spider.task.pageprocessor;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.MaoYanMovieBasicInfoModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.maoyan.CheckMovieService;
import com.xiaotu.server.service.maoyan.MaoYanMovieBasicInfoService;
import com.xiaotu.server.service.relation.TVDataSourceRelationService;
import com.xiaotu.spider.maoyan.pageprocessor.MaoYanMovieBasicInfoPageProcessor;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;


/***
 * 更新猫眼网站剧已变化的dataid   （且重新爬取基本信息 ）
 * @author Administrator
 *
 */
@Component("UpdateMaoYanDataIdPageProcessor")
public class UpdateMaoYanDataIdPageProcessor extends MaoYanMovieBasicInfoPageProcessor {

	private String [] START_URL = {
			"https://www.baidu.com/"
	};
	
	private Integer tvType = 6;//剧类型 :网络大电影
	
	private String resourceSiteId = Constants.SiteName.MAOYAN;//数据来源
	
	@Autowired
	private TVDataSourceRelationService tvDataSourceRelationService;
	
	@Autowired
    private MaoYanMovieBasicInfoService maoYanMovieBasicInfoService;
	
	private String business_url = "https://piaofang.maoyan.com/search?key=%s";
	
	@Autowired
	private CheckMovieService checkMovieService;
	
	private static Map<String,Object> paramMap = null;
	
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> extrasMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case "search":
                this.matchTvProcess(page,extrasMap);
                break;
            case Constants.PageDataType.BASICINFO:
                super.basicinfoProcess(page,extrasMap);
                break;
            case "_v_=yes":
            	this.basicinfoOtherProcess(page,extrasMap);
            	break;
            case "/companylist":
	            super.basicinfoCompanylistProcess(page,extrasMap);
	            break;
            case "/celebritylist":
            	super.basicinfoPeoplelistProcess(page,extrasMap);
            	break;
            case "/celebrity?id":
            	super.starBasicinfoProcess(page,extrasMap);
            	break;
            case "/companydetail":
            	super.companyBasicinfoProcess(page,extrasMap);
            	break;
            case "/artist":
            	super.companySignActorProcess(page,extrasMap);//签约艺人
            	break;	
            default:
            	this.addRequestProcess(page);
            	break;
        }
	}

	// 用于添加请求
	@Override
	public void addRequestProcess(Page page) {
		
		List<Map<String,Object>> list = checkMovieService.queryMaoYanMovieDataIdIsChange(paramMap);
		
		if (null != list && list.size() > 0) {
			
			for (Map<String,Object> obj : list) {

				//剧数据源中间表id
				String tvDataSourceId = obj.get("tvdatasourceid").toString();
				//剧checkid
				String checkId = obj.get("checkid").toString();
				//猫眼网站旧数据dataid
				String maoyOldId = obj.get("maoy_oldid").toString();
				String tvName = obj.get("tvname") == null?"":obj.get("tvname").toString();
				String actor = obj.get("actor") == null?"":obj.get("actor").toString();
				String director = obj.get("director") == null?"":obj.get("director").toString();
				String relaseDate = obj.get("relasedate") == null?"":obj.get("relasedate").toString();

				Map<String, Object> extraMap = new HashMap<String, Object>();
				extraMap.put("tvDataSourceId", tvDataSourceId);
				extraMap.put("checkId", checkId);
				extraMap.put("maoyOldId", maoyOldId);
				extraMap.put("tvName", tvName);
				extraMap.put("actor", actor);
				extraMap.put("director", director);
				extraMap.put("relaseDate", relaseDate);
				if (StringUtils.isBlank(tvName))
					return;
				PageUtils.addTargetRequest(page, String.format(business_url, KeywordEncode.encode(tvName)), extraMap);
			}
		}
	}
	
	
	@Override
	public String getPageDataType(Page page) {
		String url = page.getRequest().getUrl();
		if(url.contains("/search?key")){
			return "search";
		}else if (url.contains("moresections")){
        	return Constants.PageDataType.BASICINFO;
        }else if (url.contains("_v_=yes")){
        	return "_v_=yes";
        }else if (url.contains("/companylist")){
        	return "/companylist";
        }else if (url.contains("/celebritylist")){
			return "/celebritylist";
		}else if(url.contains("/celebrity?id")){
			return "/celebrity?id";
		}else if(url.contains("/companydetail")){
			String tempUrl = url.substring(url.lastIndexOf("/")+1).replaceAll("[0-9]","");
			if(tempUrl.equals("")){
				return "/companydetail";
			}else if(tempUrl.equals("artist")){
				return "/artist";
			}
		}
		return "addRequest";
	}
	
	
	//网剧基本信息
	public void basicinfoOtherProcess(Page page, Map<String, Object> extrasMap){
		
		MaoYanMovieBasicInfoModel maoYanMovieBasicInfoModel = (MaoYanMovieBasicInfoModel) extrasMap.get("movieBasicInfo");
		
		String url = page.getRequest().getUrl();
		
		String dataId = extrasMap.get("dataId").toString();
		
		if(url.contains(dataId)){
			//类型
			String [] label = null;
			//时长
			String timeLength = null;
			//上映日期
			String showingDate = null;
			List<String> pTagList = page.getHtml().xpath("//section[@class='summary-section']//div[@class='info-detail']//div[@class='info-list']//p").all();
			if(null != pTagList && pTagList.size()>0){
				for(String pTag : pTagList){
					Html pTagHtml = new Html(pTag);
					if(null != pTagHtml.xpath("//p/@class") && pTagHtml.xpath("//p/@class").get().contains("info-type ellipsis-1")){
						label = HtmlUtil.delHTMLTag(pTagHtml.xpath("//p").get()).replaceAll("[ ]", "").split("/");
						if(label.length == 0){
							label = null;
						}
					}
					if(null != pTagHtml.xpath("//p/@class") && pTagHtml.xpath("//p/@class").get().contains("info-duration ellipsis-1")){
						timeLength = pTagHtml.xpath("//p//text()").get().replaceAll("[分钟]", "");
					}
					if(null != pTagHtml.xpath("//p/@class") && pTagHtml.xpath("//p/@class").get().contains("info-release ellipsis-1")){
						showingDate = pTagHtml.xpath("//p//text()").get();
						if(showingDate.lastIndexOf("上线")!=-1){
							showingDate = showingDate.substring(0,showingDate.indexOf("上线"));
						}
						showingDate = showingDate.replaceAll("[\u4e00-\u9fa5-]", "");
						if(showingDate.length()==8){
							showingDate = super.convertDateStr(showingDate);
						}
					}
					
				}
			}
			
			if(null == timeLength){
				
				if(null != page.getHtml().xpath("//section[@class='summary-section']"
						+ "//div[@class='info-detail']"
						+ "//div[@class='info-source-duration']"
						+ "//p[@class='.ellipsis-1']//span//text()").get()
						&& StringUtils.isNotBlank(page.getHtml().xpath("//section[@class='summary-section']"
						+ "//div[@class='info-detail']"
						+ "//div[@class='info-source-duration']"
						+ "//p[@class='.ellipsis-1']//span//text()").get())){
					timeLength = page.getHtml().xpath("//section[@class='summary-section']"
							+ "//div[@class='info-detail']"
							+ "//div[@class='info-source-duration']"
							+ "//p[@class='.ellipsis-1']//span//text()").get().trim().replaceAll("[ 分钟]", "");
				}
				
			}
			
			//首先判断是否是网大（6）
			String netMovieFlag = page.getHtml().xpath("//div[@class='sections tv-page']").get();
			if(null != netMovieFlag){
				
				//修改
				tvDataSourceRelationService.updateMaoYanDataId(extrasMap);
				maoYanMovieBasicInfoModel.setLabel(label);
				maoYanMovieBasicInfoModel.setTimeLength(timeLength);
				maoYanMovieBasicInfoModel.setShowingDate(showingDate);
				extrasMap.put("movieBasicInfo", maoYanMovieBasicInfoModel);
				super.basicinfoOtherProcess(page, extrasMap);
			}
		}
	}
	
	
	//将骨朵影视剧与猫眼影视剧进行匹配
	@SuppressWarnings("unused")
	private void matchTvProcess(Page page, Map<String, Object> extrasMap) {
		
		//剧数据源中间表id
		String tvDataSourceId = extrasMap.get("tvDataSourceId")==null?null:extrasMap.get("tvDataSourceId").toString();
		//剧checkid
		String checkId = extrasMap.get("checkId") == null?null:extrasMap.get("checkId").toString();
		//猫眼网站旧数据dataid
		String maoyOldId = extrasMap.get("maoyOldId") == null?null:extrasMap.get("maoyOldId").toString();
		
		//骨朵剧名称
		String gu_tvName = extrasMap.get("tvName")==null?null:extrasMap.get("tvName").toString();
		//猫眼剧上映时间
		String gu_tvRelaseDate = extrasMap.get("relaseDate")==null?null:extrasMap.get("relaseDate").toString().replaceAll("[- ]", "");
		//猫眼剧导演
		String gu_director = extrasMap.get("director")==null?null:extrasMap.get("director").toString().replaceAll("[,，]", "/");
		//猫眼剧主演
		String gu_actor = extrasMap.get("actors")==null?null:extrasMap.get("actors").toString().replaceAll("[,，]", "/");
		//骨朵剧的网站数据id
		String gu_dataId = extrasMap.get("dataId")==null?null:extrasMap.get("dataId").toString();
		
		
		List<String> articleTagList = page.getHtml().xpath("//div[@id='search-list']//article").all();
		
		List<Map<String,Object>> appearanceMapList = new ArrayList<Map<String,Object>>();
		
		//正确率最高的
		String maxAppearanceMaoDataId = "";
		String maxAppearance = "";
		String maxAppearanceTvname = "";
		
		for(String article : articleTagList){
			
			Map<String,Object> appearanceMap = new HashMap<String,Object>();
			
			Html articleHtml = new Html(article);
			//猫眼剧名称
			String mao_tvName = null;
			//猫眼剧上映时间
			String mao_tvRelaseDate = null;
			//猫眼剧导演
			String mao_director = null;
			//猫眼剧主演
			String mao_actor = null;
			//猫眼剧的网站数据id
			String mao_dataId = null;
			
			//猫眼剧名称
			mao_tvName = articleHtml.xpath("//div[@class='title']//text()").get().trim();
			//猫眼剧上映时间
			if(null != articleHtml.xpath("//p//text()").all().get(0)){
				mao_tvRelaseDate = articleHtml.xpath("//p//text()").all().get(0).trim().replaceAll("[\u4e00-\u9fa5- ]", "");
			}
			
			//猫眼剧导演
			if(null != articleHtml.xpath("//p[@class='lineDot']//span[@class='movie-director']//text()").get()){
				mao_director = convertStr(articleHtml.xpath("//p[@class='lineDot']//span[@class='movie-director']//text()").get().trim());
			}
			
			//猫眼剧主演
			if(null == mao_director && null != articleHtml.xpath("//p[@class='lineDot']//span//text()").all().get(0)){
				mao_actor = convertStr(articleHtml.xpath("//p[@class='lineDot']//span//text()").all().get(0).trim());
			}
			if(null != mao_director && null != articleHtml.xpath("//p[@class='lineDot']//span//text()").all().get(1)){
				mao_actor = convertStr(articleHtml.xpath("//p[@class='lineDot']//span//text()").all().get(1).trim());
			}
			
			//猫眼剧的网站数据id
			if(null != articleHtml.xpath("//article[@class='indentInner canTouch']/@data-url").get()){
				mao_dataId = articleHtml.xpath("//article[@class='indentInner canTouch']/@data-url").get().replaceAll("[a-z/]", "");
			}
			
			
			int appearance = 0;
			//剧名
			if(StringUtils.isNotBlank(mao_tvName) && StringUtils.isNotBlank(gu_tvName)){
				if(StringUtil.stringFilter(mao_tvName).equals(StringUtil.stringFilter(gu_tvName))){
					appearance++;
					
					//上映时间
					if(StringUtils.isNotBlank(gu_tvRelaseDate) && StringUtils.isNotBlank(mao_tvRelaseDate)
							&& gu_tvRelaseDate.equals(mao_tvRelaseDate)){
						appearance++;
					}
					//导演
					if(StringUtils.isNotBlank(gu_director) && StringUtils.isNotBlank(mao_director)
							&& (isContains(mao_director,gu_director) || isContains(gu_director,mao_director))){
						appearance++;
					}
					//演员
					if(StringUtils.isNotBlank(gu_actor) && StringUtils.isNotBlank(mao_actor)
							&& (isContains(mao_actor,gu_actor) || isContains(gu_actor,mao_actor))){
						appearance++;
					}
					
				}
			}
			
			appearanceMap.put("mao_dataId", mao_dataId);
			appearanceMap.put("appearance", appearance);
			appearanceMap.put("mao_tvName", mao_tvName);
			appearanceMapList.add(appearanceMap);
		}
		
		if(appearanceMapList.size()>0){
			Collections.sort(appearanceMapList, new Comparator<Map<String,Object>>() {

				@Override
				public int compare(Map<String, Object> o1, Map<String, Object> o2) {
					
					int v1 = Integer.parseInt(o1.get("appearance").toString()); 
					int v2 = Integer.parseInt(o2.get("appearance").toString());
					
						if((v2-v1)>0){
							return 1;
						}else if((v2-v1)<0){
							return -1;
						}else{
							return 0;
						}
				}
			});
			
			//正确率最高的
			maxAppearanceMaoDataId = appearanceMapList.get(0).get("mao_dataId").toString();
			maxAppearance = appearanceMapList.get(0).get("appearance").toString();
			maxAppearanceTvname = appearanceMapList.get(0).get("mao_tvName").toString();
			
			int appearance = Integer.parseInt(appearanceMapList.get(0).get("appearance").toString());
			if(appearance>=1){//最低条件名字匹配
				maxAppearanceMaoDataId = appearanceMapList.get(0).get("mao_dataId").toString();
				if(StringUtils.isNotBlank(maxAppearanceMaoDataId)){
					//爬取猫眼其他相关信息
					Map<String,Object> paramsMap = new HashMap<String,Object>();
					paramsMap.put("dataId", maxAppearanceMaoDataId);
					paramsMap.put("movieName", maxAppearanceTvname);
					paramsMap.put("resourceSiteId", Constants.SiteName.MAOYAN);
					
					paramsMap.put("tvDataSourceId", tvDataSourceId);
					paramsMap.put("checkId", checkId);
					paramsMap.put("maoyOldId", maoyOldId);
					
					
					//爬取猫眼
					MaoYanMovieBasicInfoModel movieBasicInfo = maoYanMovieBasicInfoService.queryMovieBasicInfoList(paramsMap);
					if(null == movieBasicInfo){
						super.tvType = tvType;
						super.resourceSiteId = resourceSiteId;
						PageUtils.addTargetRequest(page, String.format("https://piaofang.maoyan.com/movie/%s/moresections", maxAppearanceMaoDataId), paramsMap);
					}
				}
			}
		}
		
	}
	
	
	private boolean isContains(String str1,String str2){
		
		boolean flag = false;
		String[] str1Arr = str1.split("/");
		String[] str2Arr = str2.split("/");
		for(String s1 : str1Arr){
			for(String s2 : str2Arr){
				if(s1.contains(s2)){
					flag = true;
					return flag;
				}
			}
		}
		
		return flag;
	}
	
	private String convertStr(String paramStr){
		
		if(paramStr.length()<=0){
			return null;
		}
		if(StringUtils.isNotBlank(paramStr)){
			return paramStr.replaceAll("[导演 演员：]", "").replaceAll("[，,]", "/");
		}
		return null;
	}
	
	@Override
	protected String[] getStartRequestUrls() {
		return START_URL;
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_BOXOFFICE;
	}


}
